#from urllib import request
from datetime import datetime

from werkzeug.urls import url_parse

from app import App, db
from flask import render_template, redirect, flash, url_for
from app.forms import LoginForm,RegistrationForm
from flask_login import current_user, login_user, logout_user, login_required
from app.modules import User

@App.before_request
def before_request():
    if current_user.is_authenticated:
        current_user.last_seen=datetime.utcnow()
        db.session.commit()

@App.route('/')
@App.route('/index')
@login_required
def index():
    #user={'username':'wyy'}
    '''
    post=[
        {
            'author':{'username':'John'},
            'body':'Beautiful day!'
        },
        {
            'author': {'username': 'Summer'},
            'body': 'Bad day!'
        }
    ]
    '''
    #return render_template('index.html',title="home",user=user)
    return render_template('index.html', title="home")





@App.route('/loginxx',methods=['GET','POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    login_form = LoginForm()
    if login_form.validate_on_submit():  # post请求
        user=User.query.filter_by(username=login_form.username.data).first()
        if user is None or user.check_password(login_form.password.data):
            flash('invalid password!')
        login_user(user,login_form.remember_me.data)
        from flask import request
        next_page=request.args.get('next')
        if not next_page or url_parse(next_page).netloc=='':
            next_page=url_for('index')
        return redirect(next_page)
    return render_template('login.html', title="Sign in", form=login_form)
    pass
@App.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('index'))
    #return render_template('login.html', title="Sign in", form=login_form)
    pass
'''
def login():
    login_form = LoginForm()
    if login_form.validate_on_submit():#post请求
        msg='Login requested for user={},password={},remember_me={}'.format(login_form.username.data,login_form.password.data,login_form.remember_me.data)
        flash(msg)
        print(msg)
        return redirect(url_for('index'))
    return render_template('login.html', title="Sign in", form=login_form)
'''

@App.route('/register',methods=['GET','POST'])
def register():
    #如果是登陆的就返回index
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form=RegistrationForm()
    if form.validate_on_submit():
        user=User(username=form.username.data,email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('Congratulations,you are now a register user')
        return redirect(url_for('login'))
    return render_template('register.html',title='Register',form=form)

@App.route('/user/<username>')
@login_required
def user(username):
    user=User.query.filter_by(username=username).first_or_404()
    posts = [
        {
            'author': user,
            'body': 'test post 1'
        },
        {
            'author':user,
            'body': 'test post 2'
        }
    ]
    return render_template('user.html',user=user,posts=posts)

